<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<form id="form_department_input" class="form-horizontal">
  <input type="hidden" name="keyId" id="keyId" value="${keyId}"/>
  <input type="hidden" name="_departmentId" id="_departmentId" value="${departmentId}"/>
  <div id="container_department_input"></div>
</form>

<script id="templates_department_input" type="text/x-jsrender">

  {{if parentId}}
    <div class="form-group">
      <label class="col-md-3 control-label">上级部门</label>
      <div class="col-md-7">
        <input type="hidden" name="parentId" id="parentId" value="{{:parentId}}"/>
        <input type="text" name="parentName" id="parentName" value="{{:parentName}}" class="form-control" disabled>
      </div>
    </div>
  {{/if}}
  <div class="form-group">
    <label class="col-md-3 control-label">序号</label>
    <div class="col-md-7">
      <input type="number" name="sortNo" id="sortNo" value="{{:sortNo}}" class="form-control" placeholder="请输入序号">
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label">部门名称</label>
    <div class="col-md-7">
      <input type="text" name="name" id="name" value="{{:name}}" class="form-control" placeholder="请输入部门名称">
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label">岗位描述</label>
    <div class="col-md-7">
      <input type="text" name="description" id="description" value="{{:description}}" class="form-control" placeholder="请输入部门描述">
    </div>
  </div>

</script>

<script>

  $(function(){

    // 加载数据
    fn_department_input_load();

  });

  // 初始化表单数据
  function fn_department_input_load() {
    var $templates = $.templates('#templates_department_input');
    var $container = $('#container_department_input');
    var keyId = $('#keyId').val();
    var departmentId = $('#_departmentId').val();

    var url;
    var data;
    if (keyId) {
      url = '<%=path%>/main/department/read/' + keyId;
    } else {
      url = '<%=path%>/main/department/init';
      if (departmentId) {
        data = {departmentId: departmentId};
      }
    }
    __ajax_get(url, data, function(data) {
      if (data.state == 1) {
        var entity = data.data;

        // 初始化模板
        var html = $templates.render(entity);
        $container.html(html);

        // 表单数据校验
        fn_department_input_validate();

      }
    });
  }

  // 表单数据校验
  function fn_department_input_validate() {
    __init_validate('form_department_input', {
      rules : {
        name : {
          required : true
        }
      },
      messages : {
        name : {
          required : '请输入部门名称'
        }
      }
    });
  }

  function fn_department_input_save(callback) {
    if (!$('#form_department_input').valid()) {
      return false;
    }
    __confirm_dialog(null, '确定提交么?',
            function () {
              __form_save('form_department_input', '<%=path%>/main/department/save', function(data) {
                if(data.state == 1){

                  callback(data.data);
                } else {
                  callback();
                }
              });
            },
            function () {
              callback();
            }
    );
  }

</script>